package com.yn.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName: MQUtil
 * @Description:
 * @author: 王胜
 * @date 2021/6/17
 * @Version: 1.0
 **/
public class MQUtil {
    private static Connection connection;
    private static Channel channel;
    public static Channel getChannel() throws Exception {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        connection=factory.newConnection();
        return channel=connection.createChannel();
    }
    public static void close(){
        if(connection!=null&&connection.isOpen()){
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(channel!=null&&channel.isOpen()){
            try {
                channel.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
